//
//  RBWebVC.m
//  zhongzhenglu
//
//  Created by 马军凯 on 2023/4/25.
//

#import "RBWebVC.h"
#import "LCWebViewProgressLine.h"
#import <WebKit/WebKit.h>

@interface RBWebVC ()
<WKNavigationDelegate>
//webView
@property (nonatomic, strong) WKWebView *commentWebView;
//线
@property (nonatomic, strong) LCWebViewProgressLine *progressLine;

@end

@implementation RBWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    //导航栏
    if (self.titleString) {
        self.title = self.titleString;
    }
    //创建webView
    [self createWebView];
}
#pragma mark 返回
- (void)onNavigationLeftItem {
    if ([self.commentWebView canGoBack]) {
        [self.commentWebView goBack];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark 创建webView
- (void)createWebView {
    [self commentWebView];
    //url
    NSURL *url = [NSURL URLWithString:self.urlString];
    //请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //加载
    [self.commentWebView loadRequest:request];
    //线
    [self progressLine];
}
//开始加载
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    [self.progressLine startLoadingAnimation];
}
//加载完成
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [self.progressLine endLoadingAnimation];
}
//加载出错
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    [self.progressLine endLoadingAnimation];
}

#pragma mark 懒加载
//webView
- (WKWebView *)commentWebView {
    if (_commentWebView == nil) {
        _commentWebView = [[WKWebView alloc] init];
        _commentWebView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - STATUS_AND_NAVIGATION_BAR_HEIGHT);
        _commentWebView.backgroundColor = [UIColor whiteColor];
        _commentWebView.navigationDelegate = self;
        _commentWebView.opaque = NO;
        _commentWebView.scrollView.backgroundColor = [UIColor whiteColor];
        [_commentWebView setUserInteractionEnabled:YES];
        [self.view addSubview:_commentWebView];
    }
    return _commentWebView;
}
//线
- (LCWebViewProgressLine *)progressLine {
    if (_progressLine == nil) {
        _progressLine = [[LCWebViewProgressLine alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 2)];
        [self.view addSubview:_progressLine];
    }
    return _progressLine;
}


@end
